package com.qinghe.pro.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.qinghe.utils.Page;

public class PaginationTag extends SimpleTagSupport {

	private Page page;

	private int pageRange = 10;

	private boolean choosePages = false;

	@Override
	public void doTag() throws JspException, IOException {

		StringBuilder builder = new StringBuilder();
		createHtml(builder);
		createJs(builder);

		getJspContext().getOut().write(builder.toString());
	}

    private void createHtml(StringBuilder builder) {
		long begin = Math.max(1, page.getPageNum() - pageRange/2);
	    long end = Math.min(begin + (pageRange - 1), page.getTotalPage());

		builder.append("<div class=\"pagination fmy\">\n");
		builder.append("<ul>\n");
		builder.append("<li>共<span>" + page.getTotalCount() + "</span>条</li>");
	    if (page.getPageNum() != 1) {
			builder.append("<li class=\"nextpage\"><a href=\"javascript:gotoPage(" + (page.getPageNum() - 1) + ")\">< 上一页</a></li>\n");
	    } else {
			builder.append("<li class=\"grey\"><a href=\"javascript:void(0)\">< 上一页</a></li>\n");
	    }
	    for (long i = begin; i <= end; i++) {
	    	if (i == page.getPageNum()) {
	    		builder.append("<li class=\"currentpage\">" + i + "</li>\n");
	    	} else {
	    		builder.append("<li><a href=\"javascript:gotoPage(" + i + ")\">" + i + "</a></li>\n");
	    	}
	    }
	    if (page.getPageNum() == page.getTotalPage()) {
    		builder.append("<li class=\"grey\"><a href=\"javascript:void(0)\">下一页 ></a></li>\n");
	    } else {
    		builder.append("<li class=\"nextpage\"><a href=\"javascript:gotoPage(" + (page.getPageNum() + 1) + ")\">下一页 ></a></li>\n");
	    }
		builder.append("<li>共&nbsp;" + page.getTotalPage() + "&nbsp;页&nbsp;&nbsp;");
		builder.append("到第<input type=\"text\" id=\"p_pageIndex\" value=\"" + page.getPageNum() + "\" class=\"inta tc\" />页&nbsp;");
		builder.append("<a href=\"javascript:gotoPageByBtn()\" class=\"jump\">确定</a></li>\n");
		if (choosePages) {
			builder.append("<li>每页:\n");
			builder.append("<select id=\"p_numPerPageSelect\" onchange=\"gotoPage(1)\">\n");
			builder.append("<option value=\"10\"");
			if (page.getNumPerPage() == 10) {
				builder.append(" selected");
			}
			builder.append(">10条</option>\n");
			builder.append("<option value=\"20\"");
			if (page.getNumPerPage() == 20) {
				builder.append(" selected");
			}
			builder.append(">20条</option>\n");
			builder.append("<option value=\"50\"");
			if (page.getNumPerPage() == 50) {
				builder.append(" selected");
			}
			builder.append(">50条</option>\n");
			builder.append("</select>\n");
			builder.append("</li>\n");
		}
		builder.append("</ul>\n");
		builder.append("</div>\n");
    }

    private void createJs(StringBuilder builder) {
    	builder.append("<script type=\"text/javascript\">");
    	builder.append("function gotoPage(pageNum) {");
    	builder.append("    var form = document.getElementById(\"p_pagerForm\");");
    	builder.append("    document.getElementById(\"p_pageNum\").value = pageNum;");
    	if (choosePages) {
	    	builder.append("var numPerPage = document.getElementById(\"p_numPerPageSelect\").value;");
	    	builder.append("document.getElementById(\"p_numPerPage\").value = numPerPage;");
    	}
    	builder.append("    form.submit();");
    	builder.append("}");

    	builder.append("function gotoPageByBtn() {");
    	builder.append("    var pageIndex = document.getElementById(\"p_pageIndex\");");
    	builder.append("    var pageIndexInt = parseInt(pageIndex.value);");
    	builder.append("    var totalPage = " + page.getTotalPage() + ";");
    	builder.append("    if (pageIndexInt > 0 && pageIndexInt <= totalPage) {");
    	builder.append("        gotoPage(pageIndexInt);");
    	builder.append("    } else {");
    	builder.append("        pageIndex.select();");
    	builder.append("    }");
    	builder.append("}");
    	builder.append("</script>");
    }

	public void setPage(Page page) {
		this.page = page;
	}

	public void setPageRange(int pageRange) {
		this.pageRange = pageRange;
	}

	public void setChoosePages(boolean choosePages) {
		this.choosePages = choosePages;
	}

}
